package com.woniu.microprojectserver.buildmag.controller;

import com.woniu.microcommonsserver.entity.BuildInfoEntity;
import com.woniu.microcommonsserver.entity.LpersonInfoEntity;
import com.woniu.microcommonsserver.entity.SpaceInfoEntity;
import com.woniu.microcommonsserver.param.ResponseMsg;
import com.woniu.microprojectserver.bean.BuildTreeUtils;
import com.woniu.microprojectserver.bean.TreeData;
import com.woniu.microprojectserver.bean.WithPerson;
import com.woniu.microprojectserver.buildmag.service.impl.BuildServiceImpl;
import com.woniu.microprojectserver.personmag.service.impl.PersonServiceImpl;
import com.woniu.microprojectserver.spacemag.service.impl.SpaceServiceImpl;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/build")
public class BuildController {

    @Resource
    private BuildServiceImpl buildService;

    @Resource
    private SpaceServiceImpl spaceService;

    @Resource
    private PersonServiceImpl personService;

    @GetMapping(value = "/findByRank",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findByRank(){
        List<BuildInfoEntity> lists = buildService.findByRank();
        return ResponseMsg.builder().code(200).msg("查询成功").data(lists).build();
    }

    @GetMapping(value = "/findAll",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findAll(){
        List<BuildInfoEntity> lists = buildService.findAll();
        List<TreeData> treeList = new ArrayList<>();
        for (int i = 0; i < lists.size(); i++) {
            BuildInfoEntity a = lists.get(i);
            if (a.getRank().equals(1L)) {
                TreeData td = new TreeData();
                td.setId(a.getId());
                td.setLabel(a.getName());
                td.setRank(a.getRank());
                td.setHigherId(a.getHigherId());
                td.setChildren(new BuildTreeUtils().getTree(td,lists));
                treeList.add(td);
            }
        }
        return ResponseMsg.builder().code(200).msg("查询成功").data(treeList).build();
    }

    @GetMapping(value = "/findById",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findById(String id){
        BuildInfoEntity build = buildService.findById(id);
        List<LpersonInfoEntity> list = personService.findByBuildId(id);
        WithPerson withPerson = new WithPerson();
        withPerson.setBuild(build);
        withPerson.setPeople(list);
        return ResponseMsg.builder().code(200).msg("查询成功").data(withPerson).build();
    }

    @PostMapping(value = "/add",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg add(@RequestBody BuildInfoEntity buildInfoEntity){
        boolean bool = buildService.add(buildInfoEntity);
        return ResponseMsg.builder().code(200).msg("添加成功").data(bool).build();
    }

    @PostMapping(value = "/update",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg update(@RequestBody BuildInfoEntity buildInfoEntity){
        boolean bool = buildService.update(buildInfoEntity);
        return ResponseMsg.builder().code(200).msg("修改成功").data(bool).build();
    }

    @GetMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg delete(String id){
        boolean b = deleteDo(id);
        return ResponseMsg.builder().code(200).msg("删除成功").data(b).build();
    }

    public boolean deleteDo(String id){
        List<BuildInfoEntity> list = buildService.findByHId(id);
        if(list != null && list.size() != 0 ){
            for (BuildInfoEntity buildInfoEntity : list) {
                deleteDo("" + buildInfoEntity.getId());
            }
        }
        deleteDoTwo(id);
        return true;
    }

    public void deleteDoTwo(String id){
        List<SpaceInfoEntity> list = spaceService.findByBId(id);
        if(list != null && list.size() != 0 ){
            for (SpaceInfoEntity spaceInfoEntity : list) {
                deleteDoThree("" + spaceInfoEntity.getId());
            }
        }
        boolean a =personService.deleteByBuild(id);
        if(a){
            buildService.delete(id);
        }
    }

    public void deleteDoThree(String id){
        List<SpaceInfoEntity> list = spaceService.findByHId(id);
        if(list != null && list.size() != 0 ){
            for (SpaceInfoEntity spaceInfoEntity : list) {
                deleteDoThree("" + spaceInfoEntity.getId());
            }
        }
        boolean a =personService.deleteBySpace(id);
        if(a){
            spaceService.delete(id);
        }
    }

}
